package com.baidu.shoppingmall.comment.controller;

import com.baidu.shoppingmall.comment.constant.ShowPictures;
import com.baidu.shoppingmall.comment.demo.Demo01;
import com.baidu.shoppingmall.comment.domain.CommentInfoDTO;
import com.baidu.shoppingmall.comment.domain.CommentInfoPO;
import com.baidu.shoppingmall.comment.domain.CommentInfoVO;
import com.baidu.shoppingmall.comment.service.CommentAggregateService;
import com.baidu.shoppingmall.comment.service.CommentInfoService;
import com.baidu.shoppingmall.comment.service.CommentPictureService;
import com.baidu.shoppingmall.comment.service.CommentTimerService;
import com.baidu.shoppingmall.comment.service.impl.CommentTimerServiceImpl;
import com.baidu.shoppingmall.comment.timedtask.CommentTimer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 评论管理模块的Controller
 * @author yjf
 *
 */
@RestController
@RequestMapping("/comment")
@SuppressWarnings("all")
@EnableScheduling
public class CommentController {
	
	private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

	private static int i=0;

	/**
	 * 评论信息管理模块的service
	 */
	@Autowired
	private CommentInfoService commentInfoService;
	/**
	 * 评论晒图管理模块的service
	 */
	@Autowired
	private CommentPictureService commentPictureService;
	/**
	 * 评论统计信息管理模块的service
	 */
	@Autowired
	private CommentAggregateService commentAggregateService;

	@Autowired
	private CommentTimer commentTimer;

	@Autowired
	private CommentTimerService commentTimerService;

	
	/**
	 * 手动发表评论
	 * @param commentInfoVO 评论信息VO对象
	 * @return 处理结果
	 */
	@PostMapping("/insert")
	public Boolean publishComment(HttpServletRequest request,
								  CommentInfoVO commentInfoVO, MultipartFile[] files) {
		try {
			// 为评论设置是否晒图
			Integer showPictures = ShowPictures.NO;

			if(files != null && files.length > 0) {
				for(MultipartFile file : files) {
					if(file != null) {
						showPictures = ShowPictures.YES;
						break;
					}
				}
			}
			//设置晒图
			commentInfoVO.setShowPictures(showPictures);
			
			// 保存评论信息
			CommentInfoDTO commentInfoDTO = commentInfoVO.clone(CommentInfoDTO.class);
			commentInfoService.saveCommentInfo(commentInfoDTO);

//			//用户评论后  改变评论状态为手动评论(该接口为自己老师布置作业时自己写的接口)
//			Boolean i=commentTimerService.timingChangeState(commentInfoVO.getOrderInfoId());

			//处理图片  保存图片地址
			commentPictureService.saveCommentPictures(commentInfoDTO.getId(), files);

			// 更新评论统计信息
			commentAggregateService.refreshCommentAggregate(commentInfoDTO);

		} catch (Exception e) {
			logger.error("error", e); 
			return false;
		}
		return true;
	}


//	/**
//	 * 模拟购买商品后  绑定一个定时任务  (该接口为自己老师布置作业时自己写的接口)
//	 * @return
//	 */
//	@PostMapping("/purchase")
//	public String purchaseItems(CommentInfoPO commentInfoPO){
//		//将订单id和手动评论还是自动评论的状态添加到记录到定时表中（comment_timer表）  现在相当于只是购买商品
//		//所以默认应该为自动评论 即1
//		commentTimerService.commentAutomatic(commentInfoPO.getOrderInfoId());
//		commentTimer.checkValidityData(commentInfoPO,40000L);
//		return null;
//	}
}
